library(tidyverse)
library(janitor)
library(haven)
library(concordance)
###### 1:m matching
wits_india <- read_csv("~/Dropbox/Mokhtar-Scott/India_Project_ReStat_Publication/replication/data/raw/wits/DataJobID-2510802_2510802_INDtotal.csv") %>% 
  clean_names() %>% 
  pivot_wider(names_from = c(partner_iso3, partner_name), values_from = trade_value_in_1000_usd) %>% 
  mutate(CHN_China = replace_na(CHN_China,0)) %>% 
  mutate(id = row_number()) 
###  
wits_lmi <- read_csv("~/Dropbox/Mokhtar-Scott/India_Project_ReStat_Publication/replication/data/raw/wits/DataJobID-2510805_2510805_lmincome.csv") %>% 
  clean_names() %>% 
  pivot_wider(names_from = c(partner_iso3, partner_name), values_from = trade_value_in_1000_usd) %>% 
  mutate(CHN_China = replace_na(CHN_China,0)) %>% 
  mutate(id = row_number()) 
###
wits_ind_isic4 <- concord(sourcevar = wits_india$product_code, origin = "ISIC3", destination = "ISIC4", dest.digit = 4, all = T)
wits_ind_isic4_reduced <- do.call(rbind.data.frame,
                                  lapply(names(wits_ind_isic4), 
                                         function(n) 
                                           data.frame(ISIC3=n, bind_rows(wits_ind_isic4[[n]]))
                                  )) %>% 
  distinct()
wits_india_isic4_matching <- left_join(wits_india, wits_ind_isic4_reduced, by = c("product_code" = "ISIC3")) %>% 
  mutate(CHN = CHN_China * weight,
         WLD = WLD_World * weight) %>% 
  group_by(match, year) %>% 
  summarise(IND_CHN = sum(CHN),
            IND_WLD = sum(WLD)) %>% 
  drop_na() %>% 
  ungroup() %>% 
  rename(ISIC4 = "match")
###
wits_ivs_isic4 <- concord(sourcevar = wits_lmi$product_code, origin = "ISIC3", destination = "ISIC4", dest.digit = 4, all = T)
wits_ivs_isic4_reduced <- do.call(rbind.data.frame,
                                  lapply(names(wits_ivs_isic4), 
                                         function(n) 
                                           data.frame(ISIC3=n, bind_rows(wits_ivs_isic4[[n]]))
                                  )) %>% 
  distinct()
wits_lmi_isic4_matching <- left_join(wits_lmi, wits_ivs_isic4_reduced, by = c("product_code" = "ISIC3")) %>% 
  mutate(CHN = CHN_China * weight,
         WLD = WLD_World * weight) %>% 
  group_by(match, year) %>% 
  summarise(LMI_CHN = sum(CHN),
            LMI_WLD = sum(WLD)) %>% 
  drop_na() %>% 
  ungroup() %>% 
  rename(ISIC4 = "match")
##
wits_final <- inner_join(wits_india_isic4_matching, wits_lmi_isic4_matching) %>% 
  mutate(LMI_WLD_EXCL_IND = LMI_WLD - IND_WLD,
         LMI_CHN_EXCL_IND = LMI_CHN - IND_CHN)
write_dta(wits_final, "~/Dropbox/Mokhtar-Scott/India_Project_ReStat_Publication/replication/data/clean/trade_data.dta", version = 14)
prowess <- read_dta("~/Dropbox/Mokhtar-Scott/India_Project_ReStat_Publication/replication/data/working/prowess_clean.dta") %>% 
  mutate(year = year -1)
prowess_wits <- left_join(prowess, wits_final, by = c("year", "nic_08_4dig" = "ISIC4"))
write_dta(prowess_wits, "~/Dropbox/Mokhtar-Scott/India_Project_ReStat_Publication/replication/data/clean/prowess_wits.dta", version = 14)